/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmVenda.java
 *
 * Created on 20/06/2011, 12:00:41
 */

package formularios;

import java.math.BigDecimal;
import dao.Resultado;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import modelo.ItemVenda;
import static dao.BancoDeDados.*;

/**
 *
 * @author Convidados1
 */
public class frmVenda extends javax.swing.JInternalFrame {
    List<ItemVenda> itemVenda = new ArrayList();
    static int codigo = 0;
    static int codigoMercadoria = 0;
    static boolean clienteAchado = false;
    /** Creates new form frmVenda */
    public frmVenda() {
        initComponents();
        iniciaCampos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bntCancelar = new javax.swing.JToggleButton();
        bntOk = new javax.swing.JToggleButton();
        jLabel5 = new javax.swing.JLabel();
        txtCodigoCliente = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtCpfCnpj = new javax.swing.JTextField();
        txtNomeMercadoria = new javax.swing.JTextField();
        bntFinalizarVenda = new javax.swing.JButton();
        bntCancelarVenda = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        txtQuantidade = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtCodigoMercadoria = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        bntCancelarProduto = new javax.swing.JButton();
        bntVenderProduto = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtNomeCliente = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtPreco = new javax.swing.JTextField();

        bntCancelar.setText("Cancelar");
        bntCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelarActionPerformed(evt);
            }
        });

        bntOk.setText("Ok");
        bntOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntOkActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 36));
        jLabel5.setText("Venda");

        txtCodigoCliente.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCodigoClienteFocusLost(evt);
            }
        });

        jLabel3.setText("CPF/CNPJ:");

        jLabel2.setText("Código:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("CLIENTE");

        bntFinalizarVenda.setText("Finalizar Venda");
        bntFinalizarVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntFinalizarVendaActionPerformed(evt);
            }
        });

        bntCancelarVenda.setText("Cancelar Venda");
        bntCancelarVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelarVendaActionPerformed(evt);
            }
        });

        jLabel8.setText("Nome:");

        txtQuantidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtQuantidadeActionPerformed(evt);
            }
        });

        jLabel7.setText("Quantidade:");

        txtCodigoMercadoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoMercadoriaActionPerformed(evt);
            }
        });
        txtCodigoMercadoria.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCodigoMercadoriaFocusLost(evt);
            }
        });

        jLabel6.setText("Código:");

        bntCancelarProduto.setText("Cancelar Produto");
        bntCancelarProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelarProdutoActionPerformed(evt);
            }
        });

        bntVenderProduto.setText("Vender Produto");
        bntVenderProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntVenderProdutoActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel4.setText("MERCADORIA");

        jLabel9.setText("Nome:");

        jLabel10.setText("Preço:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(132, 132, 132)
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jSeparator2))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtCodigoCliente))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jSeparator1))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel10))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtPreco)
                                            .addComponent(txtQuantidade)
                                            .addComponent(txtNomeMercadoria)
                                            .addComponent(txtCodigoMercadoria, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(bntOk)
                                        .addGap(18, 18, 18)
                                        .addComponent(bntCancelar))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel9))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtNomeCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                                            .addComponent(txtCpfCnpj)))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(bntVenderProduto)
                        .addGap(18, 18, 18)
                        .addComponent(bntCancelarProduto)
                        .addGap(18, 18, 18)
                        .addComponent(bntFinalizarVenda)
                        .addGap(18, 18, 18)
                        .addComponent(bntCancelarVenda)))
                .addContainerGap(167, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCpfCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntOk)
                    .addComponent(bntCancelar))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtCodigoMercadoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtNomeMercadoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntVenderProduto)
                    .addComponent(bntCancelarProduto)
                    .addComponent(bntFinalizarVenda)
                    .addComponent(bntCancelarVenda))
                .addGap(43, 43, 43))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntVenderProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntVenderProdutoActionPerformed
           ItemVenda venda = new ItemVenda();
           int mercadoria = Integer.parseInt("0" + txtCodigoMercadoria.getText());
           venda.setMercadoria(mercadoria);
           int quantidade = Integer.parseInt("0" + txtQuantidade.getText());
           venda.setQuantidade(quantidade);
           float preco = Float.parseFloat(txtPreco.getText());
           Float valor_total_item = quantidade * preco;
           venda.setValor_total_item(valor_total_item);
           venda.setCancelado(false);
           itemVenda.add(venda);
           limparCamposItem();
}//GEN-LAST:event_bntVenderProdutoActionPerformed

    private void txtQuantidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtQuantidadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtQuantidadeActionPerformed

    private void txtCodigoMercadoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoMercadoriaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodigoMercadoriaActionPerformed

    private void txtCodigoClienteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodigoClienteFocusLost


        codigo = Integer.parseInt("0" + txtCodigoCliente.getText());

        if (codigo == 0) {

            txtCodigoCliente.requestFocusInWindow();
            JOptionPane.showMessageDialog(this, "Não existe cliente cadastrado com esse codigo!");
            return;

        } else {
            
            Object[] cliente = new Object[1];
            cliente[0] = codigo;

            Resultado rs_cliente = executaConsulta("Select * from cliente where codigo = ?", cliente);

            String cpfCnpj = null;
            String nome;
            nome= (String) rs_cliente.valorCampo(0, "nome");
            if (rs_cliente.qtdRegistros() != 0) {
                 if (rs_cliente.valorCampo(0, "tipo_pessoa").equals("F")){
                     cpfCnpj = (String) rs_cliente.valorCampo(0, "cpf");
                 }else{
                     cpfCnpj = (String) rs_cliente.valorCampo(0, "cnpj");
                 }
            }
                    txtNomeCliente.setText(nome);
                    txtCpfCnpj.setText(cpfCnpj);
                    clienteAchado = true;
                    
         }
    }//GEN-LAST:event_txtCodigoClienteFocusLost

    private void bntOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntOkActionPerformed
        if (clienteAchado){
            desabilitarCampos();
        }
    }//GEN-LAST:event_bntOkActionPerformed

    private void bntCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelarActionPerformed
        limparCampos();
        habilitarCampos();
        itemVenda=null;
    }//GEN-LAST:event_bntCancelarActionPerformed

    private void txtCodigoMercadoriaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodigoMercadoriaFocusLost
        codigoMercadoria = Integer.parseInt("0" + txtCodigoMercadoria.getText());

        if (codigoMercadoria == 0) {
            txtCodigoMercadoria.requestFocusInWindow();
            JOptionPane.showMessageDialog(this, "Não existe cliente mercadoria com esse codigo!");
            return;

        } else {

            Object[] mercadoria = new Object[1];
            mercadoria[0] = codigoMercadoria;

            Resultado rs_mercadoria = executaConsulta("Select * from mercadoria where codigo = ?", mercadoria);

            String nome;
            BigDecimal preco;
            nome= (String) rs_mercadoria.valorCampo(0, "nome");
            preco =  (BigDecimal) rs_mercadoria.valorCampo(0, "preco_custo");
                    txtNomeMercadoria.setText(nome);
            String toString = preco.toString();
                    txtPreco.setText(toString);
                    normalizaCampos();
         }
    }//GEN-LAST:event_txtCodigoMercadoriaFocusLost

    private void bntCancelarProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelarProdutoActionPerformed
        limparCamposItem();
        iniciaCampos();
    }//GEN-LAST:event_bntCancelarProdutoActionPerformed

    private void bntCancelarVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelarVendaActionPerformed
        itemVenda=null;
        frmVenda.this.dispose();
    }//GEN-LAST:event_bntCancelarVendaActionPerformed

    private void bntFinalizarVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntFinalizarVendaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bntFinalizarVendaActionPerformed
    private void desabilitarCampos() {
        txtNomeCliente.setEditable(false);
        txtCpfCnpj.setEditable(false);
        bntOk.setEnabled(false);
    }

    private void iniciaCampos(){
      bntVenderProduto.setEnabled(false);
      bntCancelarProduto.setEnabled(false);
      bntFinalizarVenda.setEnabled(false);
    }

    private void normalizaCampos(){
      bntVenderProduto.setEnabled(true);
      bntCancelarProduto.setEnabled(true);
      bntFinalizarVenda.setEnabled(true);
    }
     private void limparCampos() {
        txtCodigoCliente.setText("");
        txtNomeCliente.setText("");
        txtCpfCnpj.setText("");
    }
     private void limparCamposItem(){
         txtCodigoMercadoria.setText("");
         txtNomeMercadoria.setText("");
         txtQuantidade.setText("");
         txtPreco.setText("");
     }

    private void habilitarCampos(){
        txtCodigoCliente.setEditable(true);
        txtCpfCnpj.setEditable(true);
        txtNomeCliente.setEditable(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton bntCancelar;
    private javax.swing.JButton bntCancelarProduto;
    private javax.swing.JButton bntCancelarVenda;
    private javax.swing.JButton bntFinalizarVenda;
    private javax.swing.JToggleButton bntOk;
    private javax.swing.JButton bntVenderProduto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField txtCodigoCliente;
    private javax.swing.JTextField txtCodigoMercadoria;
    private javax.swing.JTextField txtCpfCnpj;
    private javax.swing.JTextField txtNomeCliente;
    private javax.swing.JTextField txtNomeMercadoria;
    private javax.swing.JTextField txtPreco;
    private javax.swing.JTextField txtQuantidade;
    // End of variables declaration//GEN-END:variables

}
